package com.mfhcd.facepay.utils;

import android.content.Context;
import android.os.Bundle;

import com.iflytek.cloud.SpeechConstant;
import com.iflytek.cloud.SpeechError;
import com.iflytek.cloud.SpeechSynthesizer;
import com.iflytek.cloud.SynthesizerListener;

/**
 * Author：石强
 * E-Mail: shiqiang@mfhcd.com
 * Date：2019-09-29
 * 版权所有 © 现代金融控股（成都）有限公司
 * 语音播报工具类
 */
public class VoiceUtils {
    // 默认发音人
    private static String voicer = "xiaoyan";
    // 引擎类型
    private static SpeechSynthesizer speechSynthesizer;

    public static void playVoice(Context context, String content) {

        speechSynthesizer = SpeechSynthesizer.createSynthesizer(context, null);
        speechSynthesizer.setParameter(SpeechConstant.ENGINE_TYPE, SpeechConstant.TYPE_CLOUD);
        speechSynthesizer.setParameter(SpeechConstant.TTS_DATA_NOTIFY, "1");
        // 设置在线合成发音人
        speechSynthesizer.setParameter(SpeechConstant.VOICE_NAME, voicer);
        //设置合成语速
        speechSynthesizer.setParameter(SpeechConstant.SPEED, "50");
        //设置合成音调
        speechSynthesizer.setParameter(SpeechConstant.PITCH, "50");
        //设置合成音量
        speechSynthesizer.setParameter(SpeechConstant.VOLUME, "50");
        speechSynthesizer.startSpeaking(content, new SynthesizerListener() {
            @Override
            public void onSpeakBegin() {
                //开始播放
            }

            @Override
            public void onBufferProgress(int i, int i1, int i2, String s) {
                //合成进度
            }

            @Override
            public void onSpeakPaused() {
                //暂停播放
            }

            @Override
            public void onSpeakResumed() {
                //继续播放
            }

            @Override
            public void onSpeakProgress(int percent, int beginPos, int endPos) {
                //播放进度
            }

            @Override
            public void onCompleted(SpeechError speechError) {
                //播放完成
            }

            @Override
            public void onEvent(int i, int i1, int i2, Bundle bundle) {

            }
        });
    }


}
